package com.example.search.controller;

import com.example.common.exception.ExceptionCodeEnum;
import com.example.common.to.es.SkuEsModel;
import com.example.common.utils.R;
import com.example.search.service.ProductSaveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

@Slf4j
@RequestMapping("/search/save")
@RestController
public class ElasticSaveController {

    @Autowired
    private ProductSaveService productSaveService;

    /**
     * 商品上架
     * @param skuEsModels
     * @return
     */

    @RequestMapping("/product")
    public R productStatusUp(@RequestBody List<SkuEsModel> skuEsModels) {
        Boolean flag = false;
        try {
            flag = productSaveService.productStatusUp(skuEsModels);
        } catch (IOException e) {
            log.error("ElasticSaveController ==========================> {}",e.getMessage());
            return R.error(ExceptionCodeEnum.PRODUCT_UP_EXCEPTION.getCode(),ExceptionCodeEnum.PRODUCT_UP_EXCEPTION.getMessage());
        }

        if (flag == false) {
            return R.error(ExceptionCodeEnum.PRODUCT_UP_EXCEPTION.getCode(),ExceptionCodeEnum.PRODUCT_UP_EXCEPTION.getMessage());
        }
        return R.ok().put("flag",flag);
    }
}
